package com.bw.shop2208a.search

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.shop2208a.R
import com.bw.shop2208a.adapter.GoodsSearchAdapter
import com.bw.shop2208a.adapter.HistoryAdapter
import com.bw.shop2208a.databinding.ActivitySearchBinding
import com.bw.shop2208a.db.DBUtils
import com.bw.shop2208a.entity.SearchHistoryEntity
import com.bw.shop2208a.mvvm.BaseActivity
import com.google.android.material.chip.Chip

@Route(path = "/app/search")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {
    val goodsSearchAdapter= GoodsSearchAdapter()
    val historyAdapter= HistoryAdapter()
    override fun initViwe() {
        binding.rvSearch.adapter = goodsSearchAdapter
        binding.rvSearch.layoutManager = LinearLayoutManager(this)

//        binding.history.adapter = historyAdapter
//        binding.history.layoutManager = GridLayoutManager(this,3)

        binding.search.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                //点击回车时触发
                if (query!=null){
                    viewModel.search(query)
                    //保存历史记录
                    DBUtils.getMyDataBaer().getSearchDao().insertSearch(SearchHistoryEntity(0,query,System.currentTimeMillis()))
                }
                binding.ll.visibility = View.GONE
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //文本内容发生变化时触发
                binding.ll.visibility = View.VISIBLE
                return true
            }
        })
        binding.search.requestFocus()
        goodsSearchAdapter.setOnItemClickListener { adapter, view, position ->
            val get = goodsSearchAdapter.data[position]
            ARouter.getInstance().build("/app/goods").withSerializable("goods",get).navigation()
        }

//        historyAdapter.data.addAll(DBUtils.getMyDataBaer().getSearchDao().getHistory())
//        historyAdapter.notifyDataSetChanged()
//        historyAdapter.setOnItemClickListener { adapter, view, position ->
//            DBUtils.getMyDataBaer().getSearchDao().deleteSearch(historyAdapter.data[position].name)
//            historyAdapter.data.removeAt(position)
//            historyAdapter.notifyDataSetChanged()
//            ToastUtils.showLong("删除成功")
//        }
        val history = DBUtils.getMyDataBaer().getSearchDao().getHistory()
        if (history!=null){
            binding.cg.removeAllViews()
            history.forEach {
                val key = it.name
                val view = LayoutInflater.from(this).inflate(R.layout.item_chip,null)
                view.findViewById<TextView>(R.id.chip).text = it.name//给id为chip赋值
                binding.cg.addView(view)

                view.setOnClickListener {
                    binding.search.setQuery(key,true)
                }
            }
        }

    }

    override fun iniData() {
        viewModel.successSearch.observe(this){
            if (it.size>0){
                it.forEachIndexed { index, dataGoodsEntity ->
                    dataGoodsEntity.type = index%2
                }
                goodsSearchAdapter.data.clear()
                goodsSearchAdapter.data.addAll(it)
                goodsSearchAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong("未搜索到符合条件的关键词")
                goodsSearchAdapter.data.clear()
                goodsSearchAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_search

    override fun getViewModel(): Class<SearchViewModel> = SearchViewModel::class.java

}